package Utils;

import core.JitCommit;
import fileoperation.FileCreation;
import fileoperation.FileReader;
import gitobject.Blob;
import gitobject.Commit;
import gitobject.GitObject;
import gitobject.Tree;
import repository.Repository;
import sha1.SHA1;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import static gitobject.Tree.*;

public class addUtils {
    //1.当前文件是否存在序列化对象
    //2.读入index

    public static List<String> readIndex(String filepath) throws IOException {//读取index文件
        List<String> allLines = Files.readAllLines(Paths.get(filepath));
        return allLines;
    }

    public static String readAllIndex(String filepath) throws IOException {//将全部index放到String中，便于搜索
        StringBuilder stringBuilder = new StringBuilder();
        List<String> allLines = Files.readAllLines(Paths.get(filepath));
        for (String allLine : allLines) {
            stringBuilder.append(allLine + "\n");
        }
        return stringBuilder.toString();
    }

    public static File isExistObject(File file) throws Exception {//当前传入文件在objects是否有序列化对象,有则返回此文件
//        String objectPath= Repository.getGitDir() + File.separator + "objects";
        String objectPath = tempWorkTree.gitDir + File.separator + "objects";
        String curHash = SHA1.getHash(file);
        File dir = new File(objectPath);
        File[] fs = dir.listFiles();
        for (File f : fs) {
            if (f.getName().equals(curHash))
                return f;
        }
        return null;
    }

    public static File isExistObject(String filepath) throws Exception {//当前传入文件在objects是否有序列化对象,有则返回此文件
//        String objectPath= Repository.getGitDir() + File.separator + "objects";
        String objectPath = tempWorkTree.gitDir + File.separator + "objects";
        String curHash = SHA1.getHash(new File(filepath));
        File dir = new File(objectPath);
        File[] fs = dir.listFiles();
        for (File f : fs) {
            if (f.getName().equals(curHash))
                return f;
        }
        return null;
    }

    public static void serFiles() throws Exception {
        //对该路径下的文件进行一次重新组建，更新到最新的状态
        StringBuilder stringBuilder = new StringBuilder();
        serFiles(tempWorkTree.workTree, stringBuilder);
    }

    public static void serFiles(String filePath, StringBuilder stringBuilder) throws Exception {
        //递归初始化整个工作区,(修改，filepath为需要commit的部分路径)
        //如果是文件名的话，只需要
        Blob blob = null;
        Tree tree = null;
        File dir = new File(filePath);
        File[] fs = dir.listFiles();
        List fileList = sortFile(fs);
        String s = Commit.getLastCommit();
        String index=readAllIndex(tempWorkTree.indexLoc);
        for (Object o : fileList) {
            File file = (File) o;
            if (file.isFile()) {
                //这里的逻辑原来是commit执行的时候，在index中的文件一定会进入构建体系中
                //11.27修改，应该是文件在上一次提交的index中，或者文件在此次commit函数的参数filepath中
                if (index.contains(file.getPath())&&(JitCommit.lastIndex.contains(file.getPath())||file.getPath().contains(JitCommit.tempCommitpath))) {
                    System.out.println(file.getPath()+"-----"+JitCommit.tempCommitpath);
                    String curHash = SHA1.getHash(file);
                    blob = Blob.deserialize(curHash);
                    stringBuilder.append(blob.getFmt() + " " + file.getName() + " " + blob.getKey() + "\n");
                }

            } else if (file.isDirectory() && !file.getName().equals(".jit")) {

                //如果遍历到的是文件夹,先判断该文件夹是否在必经路径上，如果在则先序列化该文件夹下一层的文件，再对文件夹进行序列化
                //如果该文件夹不在，有两种情况1.此文件夹没有被添加到仓库中2.此文件夹在上一次添加后内容没有变动
                //哈希此文件夹，查看结果是否在object中出现，情况1直接跳过，情况2传递该结果对应序列化对象的list  还有本层str
                if (index.contains(file.getPath())&&(JitCommit.lastIndex.contains(file.getPath())||file.getPath().contains(JitCommit.tempCommitpath))) {
                    //这里需要改成在上一次tree中搜索当前文件目录，如果有就连接
                    //如果没有就说明这次也不用管他
                    Tree tree1 = new Tree(file);
                    if ((tree = (Tree.deserialize(tree1.getKey()))) == null) {

                        //说明此文件夹之前没有添加过，或者添加过但是发生了改变，需要重新写入
                        StringBuilder tempStrB = new StringBuilder();//用来向下传递，记录下层的情况
                        serFiles(filePath + File.separator + file.getName(), tempStrB);
                        tree = new Tree(file);
                        //stringbuilder是上一层传下来的，需要添加本层元素，tempStrB为本层传下去的，用来完善本层的value
                        stringBuilder.append(tree.getFmt() + " " + file.getName() + " " + tree.getKey() + "\n");
                        tree.setValue(tempStrB.toString());
                        tree.writeObject();
                    } else {
                        //说明该文件夹存在相同的object对象
                        stringBuilder.append(tree.getFmt() + " " + file.getName() + " " + tree.getKey() + "\n");
                    }
                }
            }
        }
    }
}
